package com.wang.designpattern.factory.reflectSimplefactory.pizzastore.store;


import com.wang.designpattern.factory.reflectSimplefactory.pizzastore.pizza.Pizza;

/**
 * 简单工厂类
 */
public class SimpleFactory {
    String packageName = "com.wang.designpattern.factory.absfactory.pizzastore.pizza.";
    // 可以通过配置文件来修改local内容
    String local = "LD";
    /**
     * 使用简单工厂模式改造抽象工厂模式
     * @param orderType
     * @return
     */
    public Pizza orderPizza(String orderType) {
        System.out.println("使用简单工厂模式改造抽象工厂模式");
        Pizza pizza = null;
        String beanName = packageName + local + titleCase(orderType.toLowerCase()) + "Pizza";
        System.out.println("beanName: " + beanName);
        try {
            Class<?> classPizza =  Class.forName(beanName);
            pizza = (Pizza) classPizza.newInstance();
        } catch (ClassNotFoundException e) {
            System.out.println("找不到对应的目标类" + e.getMessage());
        } catch (IllegalAccessException e) {
            System.out.println("IllegalAccessException异常" + e.getMessage());
            e.printStackTrace();
        } catch (InstantiationException e) {
            System.out.println("InstantiationException异常" + e.getMessage());
            e.printStackTrace();
        }
        return pizza;
    }

    public String titleCase(String str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }
}
